<template>
  <div>
    <el-button @click="showDialog = true">
      +选择课程
    </el-button>

    <!-- 1. 放一个弹出框 -->
    <el-dialog title="选择课程" :visible.sync="showDialog">
      <el-form size="mini" :inline="true">
        <el-form-item label="课程名称">
          <el-input v-model="form.keywords"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="search">搜索</el-button>
          <el-button>重置</el-button>
        </el-form-item>
      </el-form>

      <!-- table -->
      <el-table :data="list" @selection-change="handleSelectionChange">
        <el-table-column type="selection">
        </el-table-column>
        <el-table-column prop="title" label="标题">
        </el-table-column>
        <el-table-column prop="price" label="价格">
        </el-table-column>
        <el-table-column prop="course_type" label="类型">
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            {{scope.row.status==1? '上架': (scope.row.status==2?'下架':'待上架')}}
          </template>
        </el-table-column>
      </el-table>

      <!-- 翻页 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="form.page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="form.limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

      <!-- 放个页脚 -->
      <div slot="footer">
        <el-button @click="showDialog = false">取消</el-button>
        <el-button @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  data() {
    return {
      // 2. 表示弹出框是否显示
      showDialog: false,
      // 保存勾选的项
      checkedData: [],
      form: {
        course_classify_id: 0,
        course_type: '',
        integral_shop: 0,
        is_distribute: 0,
        is_divide_group: '',
        is_include_system_course: '',
        keywords: '',
        limit: 10,
        not_include_public_course: 1,
        not_include_system_course: 0,
        page: 1,
        recommend: 0,
        spell_group: 0,
        vip_type: 0
      },
      total: 0, // 总的记录数，一会调用完接口之后，得到总的值再赋值
      list: [] // 保存数据
    }
  },
  methods: {
    // 当勾选时
    handleSelectionChange(items) {
      this.checkedData = items
    },
    // 当点击翻页的数字时调用
    handleCurrentChange(page) {
      // 修改当前页
      this.form.page = page
      // 重新调用接口
      this.getData()
    },
    // 修改了每页条数
    handleSizeChange(size) {
      // 修改条数
      this.form.limit = size
      // 重新调用接口
      this.getData()
    },
    // 使用表单和翻页的参数调用接口
    getData() {
      axios.post('/course/basis/index', this.form).then(res => {
        if (res) {
          // 保存总数和数据列表
          this.total = res.data.total
          this.list = res.data.list
        }
      })
    },
    search() {
      // 先重置翻页为第1页
      this.form.page = 1
      // 重新调用接口
      this.getData()
    },
    submit() {
      // 重新修改和收集数据的格式：[{id:32,name:'xxx'}]
      const data = []
      this.checkedData.forEach(v => {
        data.push({
          id: v.id,
          name: v.title
        })
      })

      // 把勾选的传给父组件（子向父）
      this.$emit('courseOk', data)

      // 关闭弹出框
      this.showDialog = false
    }
  },
  // 组件创建完之后
  created() {
    this.getData()
  }
}
</script>
